﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Incominginformations;

namespace MyMES.Writer.API.Application.CommendHandlers.Incominginformations
{
    public class AddArrivalnoticeCommandHanders : IRequestHandler<AddArrivalnoticeCommands, APIResult<Arrivalnotice>>
    {
        private readonly IBaseRepository<Arrivalnotice> baseRepository;
        private readonly IMapper mapper;

        public AddArrivalnoticeCommandHanders(IBaseRepository<Arrivalnotice> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加到货通知单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Arrivalnotice>> Handle(AddArrivalnoticeCommands request, CancellationToken cancellationToken)
        {
            APIResult<Arrivalnotice> result = new APIResult<Arrivalnotice>();
            result.Code = APIEnum.添加成功;
            result.Msg = "添加成功";
            var map = mapper.Map<Arrivalnotice>(request);
            var sql = $"insert into Arrivalnotice values (@NoticeId,@NoticeNo,@NoticeName,@StoragePurchaseId,@SupplierId,@Deliverydate,@Contacts,@Contactway,@Contactremark,@NoticeNumber,@IsInspecs,@QualifiedNum,@InspecNumber,@MId)";
            await baseRepository.Insert(map, sql);
            return result;
        }
    }
}
